// keyboard controller

#include <stdio.h>
#include <stdlib.h>

#include "GL/glut.h"

#include "board.h"
#include "draw.h"

void keyboard(unsigned char c, int x, int y) {
	if (atPause())
	{
		if (c == ' ') togglePause();
		return;
	}
	Board &b = *getCurrentBoard();

	if (!atTitle() && !atCutScene() && !atEnding())
	switch (c) {
		// exit the game
		case 'q':
			exit(0);

		// movement for illusionist
		case 'w':		// up
			b.moveUp();
			break;

		case 's':		// down
			b.moveDown();
			break;

		case 'a':		// left
			b.moveLeft();
			break;

		case 'd':		// right
			b.moveRight();
			break;

		// powers for illusionist
		case 'h':		// food
			b.useFood();
			break;

		case 'j':		// dead body
			b.useDeadBody();
			break;

		case 'k':		// cloak
			b.useCloak();
			break;

		case 'l':		// fake illusionist
			b.useFake();
			break;

		case ' ':
			togglePause();
			break;

		default:
			break;
	}

	// when user presses any key, jump from title to main game
	if (atTitle()) {
		gotoCutScene();
	}
	else if (atCutScene())
	{
		incrementCutScene();
	}
	else if (atGameOver())
	{
		gotoTitle();
	}
	else if (atEnding() && !atCutScene()) {
		gotoTitle();
		//exit(0);
	}

//	printf("fo: %d, db: %d, cl: %d, fk: %d\n",
//		   b.getFoodUse(), b.getDeadBodyUse(), b.getCloakUse(), b.getFakeUse());

	// user input drives can change whether the player has won
	b.checkForWin();

	// if we made it here, redraw the screen
	glutPostRedisplay();
}

// end of keyboard.cpp
